<?php

namespace app\models;

use Yii;
use yii\db\Expression;
/**
 * This is the model class for table "member_activity".
 *
 * @property integer $id
 * @property integer $member_id
 * @property integer $type
 * @property string $dt
 * @property string $ip
 * @property string $activity_info
 * @property double $usd_now
 * @property double $nea_now
 *
 * @property Member $member
 */
class MemberActivity extends \yii\db\ActiveRecord
{
  const  ACTIVITY_REG_USER=1;         //        '1'=>'注册',
  const  ACTIVITY_LOGIN=2;            //        '2'=>'登录',
  const  ACTIVITY_LOGOUT=3;           //        '3'=>'登出',
  const  ACTIVITY_CHARGE=4;           //        '4'=>'充值',
  const  ACTIVITY_CASH=5;             //        '5'=>'提现',
  const  ACTIVITY_USD_TO_NEA=6;       //        '6'=>'转账USD到NEA',
  const  ACTIVITY_NEA_TO_USD=7;       //        '7'=>'转账NEA到USD',
  const  ACTIVITY_FENHONG=8;          //        '8'=>'分红（NEA）',

  const  FETCH_TYPE_COUNT_NEA=1;            //        '1'=>'NEA账户变化',
  const  FETCH_TYPE_COUNT_USD=2;            //        '2'=>'USD账户变化',
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'member_activity';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['member_id', 'type', 'ip', 'activity_info'], 'required'],
            [['member_id', 'type'], 'integer'],
            [['dt'], 'safe'],
            [['usd_now', 'nea_now'], 'number'],
            [['ip'], 'string', 'max' => 50],
            [['activity_info'], 'string', 'max' => 128],
            [['member_id'], 'exist', 'skipOnError' => true, 'targetClass' => Member::className(), 'targetAttribute' => ['member_id' => 'id']],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
          'id' => 'ID',
          'member_id' => '会员ID',
          'type' => '活动',
          'dt' => '日期时间',
          'ip' => 'IP',
          'activity_info' => '活动细节',
            'usd_now' => '个人账户余额',
            'nea_now' => '基金账户余额',
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getMember()
    {
        return $this->hasOne(Member::className(), ['id' => 'member_id']);
    }

    /**
     * 添加活动
     * @return \yii\db\ActiveQuery
     */
        public static function addActivity($memberid,$type,$detail,$nea_count,$usd_count){
          $activity = new MemberActivity();
          $activity->member_id=$memberid;
          $activity->type=$type;
          $activity->dt=new Expression('NOW()');
          $activity->ip=Yii::$app->request->getUserIP();
          $activity->activity_info=$detail;
          $activity->nea_now=$nea_count;
          $activity->usd_now=$usd_count;
          if(!$activity->save()){
            //Yii::getLogger()->log(print_r($activity->errors,true),yii\log\Logger::LEVEL_ERROR,'MyLog');
            return false;
          }
          return true;
        }

        /*
        * 查询走会员账户变化数据
        */
        public static function getMemberCountGoData($member_id,$fetch_type)
        {
          $types=[];
          if($fetch_type==MemberActivity::FETCH_TYPE_COUNT_NEA){
            $types=[
              MemberActivity::ACTIVITY_REG_USER,
              MemberActivity::ACTIVITY_USD_TO_NEA,
              MemberActivity::ACTIVITY_NEA_TO_USD,
              MemberActivity::ACTIVITY_FENHONG,
            ];
          }else if($fetch_type==MemberActivity::FETCH_TYPE_COUNT_USD){
            $types=[
              MemberActivity::ACTIVITY_REG_USER,
              MemberActivity::ACTIVITY_CHARGE,
              MemberActivity::ACTIVITY_CASH,
              MemberActivity::ACTIVITY_USD_TO_NEA,
              MemberActivity::ACTIVITY_NEA_TO_USD];
          }else{
            return '[]';
          }
          $datas=MemberActivity::find()->select(['dt','nea_now','usd_now'])->where([
            'member_id'=>Yii::$app->user->getId(),
            'type'=>$types
          ])->orderBy(['dt'=>SORT_ASC])->asArray()->all();

          //按日期排序
          /*foreach($nea_datas as $key =>$value){
            $data_date_arr[$key]=$value['dt'];
          }
          if(!empty($nea_datas)){
            array_multisort($data_date_arr,SORT_ASC,$nea_datas);
          }*/

          $dataset="[";

          foreach($datas as $data){
            $array=[];
            $array[0]=$data['dt'];
            $array[1]=number_format($data['nea_now'],2);
            $array[2]=number_format($data['usd_now'],2);
            $dataset.=json_encode($array,JSON_NUMERIC_CHECK).",";
          }
          $dataset.="]";
          return $dataset;
        }
}
